package com.gzz.orchid.common.webmvc.securityjwt;

import com.gzz.orchid.common.webmvc.support.HttpResult;
import com.gzz.orchid.common.webmvc.util.RespUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

/**
 * 认证失败处理器
 * 未认证或者认证错误 处理器
 *
 * @author higmd
 */
@Slf4j
public class DelegateAuthenticationEntryPointHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        log.error("认证失败处理器：{}", e.getMessage());
        //        response.setContentType("application/json;charset=UTF-8");
        //        response
        //                .getWriter()
        //                .println(JSON.toJSONString(HttpResult.failure().message("身份认证失败！")));
        RespUtil.resolver(response, HttpResult.failure().message("身份认证失败！"));

    }
}
